package com.yy.tools.graphics
{
	import com.yy.yyui.ButtonGroup;
	import com.yy.yyui.EmptyDisplayObject;
	import com.yy.yyui.checkbox.CheckLabel;
	import com.yy.yyui.text.TextLabel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.IBitmapDrawable;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Scene;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextFormatAlign;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	/**
	 * 图形处理常用工具
	 * @author YY
	 * 
	 */
	public class GT
	{
		private static var timers:Dictionary = new Dictionary();
		
		/**
		 *滚动位图y坐标 
		 * @param src
		 * @param y
		 * 
		 */
		private static function rollBitmapDataY(src:BitmapData,y:int):void{
			if(y==0){
				return;
			}
			y = y%src.height;
			var mat:Matrix = new Matrix();
			var bd:BitmapData;
			if(y>0){
				bd = new BitmapData(src.width,y,true,0);
				mat.translate(0,y-src.height);
				bd.draw(src,mat);
				src.scroll(0,y);
				src.fillRect(new Rectangle(0,0,src.width,y),0);
				src.draw(bd);
			}else if(y<0){
				bd = new BitmapData(src.width,-y,true,0);
				bd.draw(src);
				src.scroll(0,y);
				src.fillRect(new Rectangle(0,src.height+y,src.width,-y),0);
				mat.translate(0,src.height+y);
				src.draw(bd,mat);
			}			
		}
		/**
		 *滚动位图x坐标 
		 * @param src
		 * @param x
		 * 
		 */
		private static function rollBitmapDataX(src:BitmapData,x:int):void{
			if(x==0){
				return;
			}
			x = x%src.width;
			var mat:Matrix = new Matrix();
			var bd:BitmapData;
			if(x>0){
				bd = new BitmapData(x,src.height,true,0);
				mat.translate(x-src.width,0);
				bd.draw(src,mat);
				src.scroll(x,0);
				src.fillRect(new Rectangle(0,0,x,src.height),0);
				src.draw(bd);
			}else if(x<0){
				bd = new BitmapData(-x,src.height,true,0);
				bd.draw(src);
				src.scroll(x,0);
				src.fillRect(new Rectangle(src.width+x,0,-x,src.height),0);
				mat.translate(src.width+x,0);
				src.draw(bd,mat);
			}
		}
		
		/**
		 *滚动位图 
		 * @param src
		 * @param x
		 * @param y
		 * 
		 */
		public static function rollBitmapData(src:BitmapData,x:int,y:int):void{
			rollBitmapDataX(src,x);
			rollBitmapDataY(src,y);
		}
		
		/**
		 *给物体描边 
		 * @param sp 
		 * @param lineColor
		 * @param lineWeigh
		 * @return 
		 * 
		 */
		public static function lineBorder(sp:Sprite, lineColor: uint = 0x000000, lineWeigh: uint = 1): Sprite
		{
			sp.graphics.beginFill(lineColor);
			sp.graphics.drawRect(-lineWeigh, -lineWeigh, sp.width + 2 * lineWeigh, sp.height + 2 * lineWeigh);
			sp.graphics.endFill();			
			return sp;	
		}

		
		/**
		 * 根据source对象的 W 和 H 和 size 大小创建外框
		 * parent:支持source的容器
		 * source:目标对象
		 * size:框架的大小(像素)
		 */ 
		public static function createBorderByDisplayObjcet(parent:DisplayObjectContainer,source:DisplayObject,size:Number, color:uint=0xd1cfcc):DisplayObject
		{
			var sp:Sprite;
			var dobj:DisplayObject;
			sp = new Sprite;
			var child:Shape = new Shape();
			child.graphics.lineStyle(1,0xd1cfcc);
			child.graphics.beginFill(0xd1cfcc);
			child.graphics.drawRect(0,0,source.width + size*1.5,source.height + size*1.5);
			child.graphics.endFill();
			sp.addChild(child);
			dobj = sp.addChild(source);
			dobj.x = size;
			dobj.y = size;
			return parent.addChild(sp);
		}
		

		 
//////////////////////////////////YY////////////////////////////////////////////////////////////		 
		 /**
		  *垂直排布元素 
		  * @param parent 父容器
		  * @param objects 显示对象
		  * @param vGap 垂直距离
		  * @param top y的起始坐标
		  * @param x 水平坐标
		  * @param width 设置宽度
		  * @return 
		  * 
		  */		
		 public static function verticalLayout(parent:DisplayObjectContainer,objects:Array=null,vGap:Number=0,top:Number=0,x:Object=null,width:Object=null):Number{
			 var i:int,l:int;
			 var y:Number = top;
			 if(objects==null){
				 if(parent == null){
					 throw new Error("verticalLayout():objects and parent cannot all be null");
				 }
				 i = 0,l = parent.numChildren;
				 objects = new Array();
				 for(i=0;i<l;i++){					
					 objects[i] = parent.getChildAt(i);
				 }
			 }
			 
			 for(i=0,l=objects.length;i<l;i++){
				 var dobj:DisplayObject = objects[i] as DisplayObject;
				 var h:Number = 0;
				 if(dobj!=null){
					 if(x!=null){
						 dobj.x = Number(x);
					 }
					 dobj.y = y;
					 if(width!=null){
						 dobj.width = Number(width);
					 }
					 if(parent!=null&&!parent.contains(dobj)){
						 parent.addChild(dobj);
					 }
					 if(dobj.visible){
						 h = dobj.height+vGap;
					 }
				 }
				 y+=h;
			 }
			 return y-vGap;
		 }
		 
		 /**
		  *水平排布元素 
		  * @param parent 父容器
		  * @param objects 现实对象
		  * @param hGap 水平距离
		  * @param left
		  * @param y
		  * @param height
		  * 
		  */		
		 public static function horizontalLayout(parent:DisplayObjectContainer,objects:Array=null,hGap:Number=0,left:Number=0,y:Object=null,height:Object=null):void{
			 var i:int,l:int;
			 var x:Number = left;
			 if(objects==null){
				 if(parent == null){
					 throw new Error("horizontalLayout():objects and parent cannot all be null");
				 }
				 i = 0;
				 l = parent.numChildren;
				 objects = new Array();
				 for(i=0;i<l;i++){
					 objects[i] = parent.getChildAt(i);
				 }				
			 }
			 l = objects.length;
			 for(i=0;i<l;i++){
				 var dobj:DisplayObject = objects[i] as DisplayObject;
				 var w:Number = 0;
				 if(dobj!=null){
					 dobj.x = x;
					 if(y!=null){
						 dobj.y = Number(y);
					 }
					 //设置现实对象的高
					 if(height!=null){
						 dobj.height = Number(height);
					 }
					 //添加到父容器
					 if(parent!=null&&!parent.contains(dobj)){
						 parent.addChild(dobj);
					 }
					 if(dobj.visible){
						 w = dobj.width+hGap;
					 }
				 }
				 x+=w;
			 }
		 }
		 
		 /**
		  *元件 级子元件  停止在制定帧
		  * @param mc
		  * @param frame
		  * 
		  */
		 public static function stopMovie(dobj:DisplayObjectContainer, frame:int=1):void {
			 if(dobj is MovieClip) {
				 MovieClip(dobj).gotoAndStop(frame);
			 }
			 var len:int = dobj.numChildren;
			 var i:int;
			 var obj:DisplayObjectContainer;
			 for(i=0; i<len; i++) {
				 obj = dobj.getChildAt(i) as DisplayObjectContainer
				 if(obj) {
					 if(obj.numChildren > 0) {
						 stopMovie(obj, frame);
					 }else {
						 MovieClip(obj).gotoAndStop(frame);
					 }
				 } 
			 }
		 }
		 
		 /**
		  *播放mc到指定标签 
		  * @param mc
		  * @param label
		  * 
		  */
		 public static function stopMovieToLable(mc:MovieClip, label:String):void {
			 var scene:Scene;
			 var arr:Array;
			 if (!mc || mc.totalFrames == 1){
				 return;
			 };
			 scene = mc.scenes[0];
			 arr = getFrameLabels(scene.labels);
			 if (arr.indexOf(label) == -1){
				 return;
			 };
			 mc.gotoAndStop(label);
		 }
		 /**
		  *提取标签的名字 
		  * @param objs
		  * @return 
		  * 
		  */
		 public static function getFrameLabels(objs:Array):Array{
			 var arr:Array;
			 var i:int;
			 arr = [];
			 i = 0;
			 while (i < objs.length) {
				 arr[i] = objs[i].name;
				 i++;
			 };
			 return arr;
		 }
		 
		 /**
		  *移除容器的子元素  从最高层开始移除
		  * @param dobj
		  * 
		  */
		 public static function removeAllChild(dobj:Object):void {
			 var dobjc:DisplayObjectContainer = dobj as DisplayObjectContainer;
			 if(dobjc) {
				 var index:int = dobjc.numChildren; 
				 while(index > 0) {
					 dobjc.removeChildAt(index-1);
					 index = dobjc.numChildren;
				 }
			 }
		 }
		 
		 /**
		  *删除对象的所有动态 
		  * @param actionList
		  * 
		  */
		 public static function destroyActions(actionList:Object=null):void{
			 var actions:Object = actionList==null?new Object():actionList;
			 for(var prop:* in actions){
				 delete(actions[prop]);
			 }
		 }
		 
		 
		 /**
		  * 画均匀网格
		  * @param w
		  * @param h
		  * @param rows
		  * @param cols
		  * @param lineClor
		  * @param lineSize
		  * @param lineAlpha
		  * 
		  */
		 public static function drawGrid(w:int, 
										 h:int,
										 rows:int,
										 cols:int,
										 lineClor:uint,
										 lineSize:Number=1,
										 lineAlpha:Number=1):Sprite {
			 var sp:Sprite = new Sprite();
			 var rowSize:int = Math.round(h/rows);
			 var colSize:int = Math.round(w/cols);
			 sp.graphics.lineStyle(lineSize, lineClor, lineAlpha);
			 var i:int;
			 var j:int;
			 for(i=0; i<=rows; i++) {
				 sp.graphics.moveTo(0, i*rowSize);
				 sp.graphics.lineTo(w, i*rowSize);
			 }
			 for(i=0; i<=cols; i++) {
				 sp.graphics.moveTo(i*colSize, 0);
				 sp.graphics.lineTo(i*colSize, rowSize*rows);
			 }
			 return sp;
		 }
		 
		 /**
		  *画一个矩形 
		  * @param w
		  * @param h
		  * @param lineColor
		  * @param fillColor
		  * @param lineSize
		  * @param alpha
		  * @return 
		  * 
		  */
		 public static function getRectGraphic(w:int,
											   h:int,
											   lineColor:uint,
											   fillColor:uint,
											   lineSize:int=1,
											   lineAlpha:Number=1,
											   fillAlpha:Number=1):Sprite {
			 var sp:Sprite = new Sprite(); 
			 sp.graphics.lineStyle(lineSize, lineColor, lineAlpha);
			 sp.graphics.beginFill(fillColor, fillAlpha);
			 sp.graphics.drawRect(0,0,w,h);
			 sp.graphics.endFill();
			 return sp;
		 }
		 
		 /**
		  *优化显示对象性能 
		  * 
		  */
		 public static function optimizationDisObj(obj:Object,mouseE:Boolean=false, tabE:Boolean=false, mouseC:Boolean=false, enabled:Boolean=false):void {
			 obj["mouseEnabled"] = mouseE;
			 obj["tabEnabled"] = tabE;
			 obj["mouseChildren"] = mouseC;
			 if(obj.hasOwnProperty("enabled")) {
			 	obj["enabled"] = enabled;
			 }
		 }
		 
		 /**
		  *优化多个显示对象性能 
		  * @param arr
		  * 
		  */
		 public static function optimizationDisObjs(arr:Array):void {
			 var i:int;
			 var len:int = arr.length;
			 for(i=0; i<len; i++) {
				 optimizationDisObj(arr[i]);
			 }
		 }
		 
		 /**
		  *画一个镂空的框 
		  * @param w 整体框的宽
		  * @param h 整体框的高
		  * @param color 框的颜色
		  * @param alpha 框的透明度
		  * @param rw	镂空框的宽
		  * @param rh	镂空框的高
		  * @param rx	镂空框的x坐标
		  * @param ry	镂空框的y坐标
		  * 
		  */
		 public static function getHoleRect(w:int, 
											h:int,
											color:uint,
											alpha:Number,
											rw:int,
											rh:int,
											rx:int,
											ry:int):Sprite {
			 var sp:Sprite = new Sprite();
			 sp.graphics.beginFill(color, alpha);
			 sp.graphics.drawRect(0, 0, w, h);
			 sp.graphics.drawRect(rx,ry, rw, rh);
			 sp.graphics.endFill();
			 return sp;
		 }
		 
		 
		 public static function centerScrollChild(parent:DisplayObject,child:DisplayObject):void{
			 if(parent==null||child==null) return;
			 var rect:Rectangle = child.getBounds(parent);
			 var x:Number = (rect.width - parent.width)/2;
			 var y:Number = (rect.height - parent.height)/2;
			 parent.scrollRect = new Rectangle(x,y,parent.width,parent.height);
		 }
		 
		 public static function createEmptyDisplayObject(w:int=0, h:int=0):DisplayObject {
			return new EmptyDisplayObject(w,h); 
		 }
		 
		 public static function fulFill2(dst:Object,color:uint = 0xff0000,alpha:Number = 1):void{
			 if((!dst is Sprite)&&(!dst is Shape)) return;
			 var g:Graphics = dst.graphics;
			 g.beginFill(color,alpha);
			 g.drawRect(0,0,dst.width,dst.height);
			 g.endFill();
		 }
		 
		 public static function centerLeftScrollChild(parent:DisplayObject,child:DisplayObject):void{
			 if(parent==null||child==null) return;
			 var rect:Rectangle = child.getBounds(parent);
			 var x:Number = 0;
			 var y:Number = (rect.height - parent.height)/2;
			 parent.scrollRect = new Rectangle(x,y,parent.width,parent.height);
		 }
		 
		 public static function centerRightScrollChild(parent:DisplayObject,child:DisplayObject):void{
			 if(parent==null||child==null) return;
			 var rect:Rectangle = child.getBounds(parent);
			 var x:Number = rect.width - parent.width;
			 var y:Number = (rect.height - parent.height)/2;
			 parent.scrollRect = new Rectangle(x,y,parent.width,parent.height);
		 }
		 
		 public static function visualizeBoolean(src:Boolean):DisplayObject{
			 var cl:CheckLabel = new CheckLabel();
			 cl.selected = src;
			 return cl;
		 }
		 
		 public static function visualizeString(src:Object,width:Object=null,line:Object = 1,color:Object = 0xffffff,align:Object=TextFormatAlign.CENTER):DisplayObject{
			 var tl:TextLabel = new TextLabel("",100,Number(line));
			 tl.textFormat.align = align.toString();
			 if(color == null)
			 {
				 tl.textFormat.color = 0x000000;				
			 }else
			 {
				 tl.textFormat.color = color;
			 }
			 tl.text = src.toString();
			 if(width==null){
				 tl.adjustWidth();
			 }else{
				 tl.width = width as Number;
			 }
			 return tl;
		 }
		 
		 public static function replaceChild(parent:Object,child:Object,replacedChild:Object,removeFromStage:Boolean = true,replaceLocation:Boolean = true,replaceWidth:Boolean = true,replaceHeight:Boolean = true):Object{
			 if(parent==null||
				 !(parent is DisplayObjectContainer)||
				 (child!=null&&!(child is DisplayObject))||
				 (replacedChild!=null&&!(replacedChild is DisplayObject)))
				 return null;
			 
			 var dobj:Object;
			 var index:int;
			 if(replacedChild!=null&&parent.contains(replacedChild)){
				 index = parent.getChildIndex(replacedChild);
			 } else{
				 index = parent.numChildren;
			 }
			 
			 if(replacedChild!=null&&parent.contains(replacedChild)){
				 if(removeFromStage){
					 dobj = parent.removeChild(replacedChild);
				 }else{
					 replacedChild.visible = false;
				 }
			 }
			 if(child!==null){
				 parent.addChildAt(child,index);
				 if(replacedChild!=null){
					 if(replaceLocation){
						 child.x = replacedChild.x;
						 child.y = replacedChild.y;
					 }
					 if(replaceWidth){
						 child.width = replacedChild.width;
					 }
					 if(replaceHeight){
						 child.height = replacedChild.height;
					 }
				 }
			 }
			 
			 return replacedChild;
		 }
	}
}